<?php
/**
 * Created by PhpStorm.
 * User: johnzhang
 * Date: 2019/7/30 0030
 * Time: 上午 9:13
 */

namespace moonpie\plugin\wechat_account\validate;


use think\Validate;

class WechatAccountValidator extends Validate
{
    protected $rule = [
        'title' => 'require|length:2,100',
        'gh_id' => 'require|alphaDash|unique:wechat_account,gh_id',
        'app_id' => 'require|unique:wechat_account,app_id|validAppId',
        'secret' => 'require|alphaDash',
        'token' => 'require|alphaDash',
        'encrypt_key' => 'alphaDash|length:43',
    ];
    protected $field = [
        'title' => '公众号名称',
        'gh_id' => '原始ID',
        'app_id' => 'APP ID',
        'secret' => '公众号密钥',
        'token' => '请求令牌',
        'encrypt_key' => '消息加密密钥',
    ];
    protected function validAppId($id, $rule, $data, $field, $title)
    {
        $label = "{$field}.validAppId";
        if(strpos($id, 'wx') !== 0) {
            $this->message($label, "{$title}不是有效的微信公众号appid信息");
            return false;
        }
        return true;
    }
    public function setExceptRecord($record)
    {
        return $this->scene('update', [
            'title', 'secret', 'token', 'encrypt_key',
            'gh_id' => 'require|alphaDash|unique:wechat_account,gh_id,' . $record['id'],
            'app_id' => 'require|alphaDash|unique:wechat_account,app_id,' . $record['id'],
        ]);
    }
}